// pages/all-ingredients/all-ingredients.js
const app = getApp();
const http = app.http;
const userInfoManager = app.userInfoManager;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    color: '#000000',
    background: '#ffffff',
    tabs: [],
    activeTab: 0,
    orgId: "",
    heightLine: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let personInfo = userInfoManager.getSupplierInfo();
    this.data.orgId = personInfo.orgId;
    let heightLine = app.globalData.statusBarHeight + 44;
    this.setData({ heightLine });
    http.listOfCategory(personInfo.id).then(res => {
      if (res.code == 0) {
        if (res.data.length > 0) {
          for(let i=0;i<res.data.length;i++){
            res.data[i].ingredientList=[];
          }
          this.setData({
            tabs: res.data
          });
          return http.productsList(this.data.orgId,res.data[0].id);
        } else {
          wx.showToast({
            duration: 1000,
            icon: 'none',
            title: "暂无食品哦~快去添加吧"
          })
          return Promise.reject();
        }
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    }).then(res => {
      if (res.code == 0) {
        this.setData({
          ["tabs[" + 0 + "].ingredientList"]: res.data
        });
        console.log(this.data.tabs);
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  getData(index){
    http.productsList(this.data.orgId, this.data.tabs[index].id).then(res => {
      if (res.code == 0) {
        this.setData({
          ["tabs[" + index + "].ingredientList"]: res.data,
          activeTab: index
        });
      } else {
        wx.showToast({
          duration: 1000,
          icon: 'none',
          title: res.msg
        })
      }
    })
  },
  onTabCLick(e) {
    const index = e.detail.index
    if (index != null) {
      this.getData(index);
    }
  },

  onChange(e) {
    const index = e.detail.index
    if (index != null) {
      this.getData(index);
    }
  },
  /**点击详情 */
  clickFood(event){
    wx.navigateTo({
      url: '/pages/all-ingredients/ingredients-detail?ids=' + event.currentTarget.dataset.item.id,
      success: (result) => {

      },
      fail: (e) => { console.log(JSON.stringify(e)) },
      complete: () => { }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})